import argparse
import io
import os
import sys
import unittest

from ydpic.yd_loginer import YDLoginer

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from ydpic.config import Config
from prutils.pr_requests import load_session


class MyTestCase(unittest.TestCase):
    def test_config(self):
        conf = Config("./config.ini")
        print(conf.username)
        print(conf.password)

    def test_login(self):
        config_path = os.path.abspath("./config.ini")
        conf = Config(config_path)
        s = load_session(conf.session_file, conf.proxy)

        print(conf.username)
        print(conf.password)
        work_dir = os.path.join(os.path.dirname(os.path.abspath("./config.ini")), "tmp")
        ydl = YDLoginer(s, conf.username, conf.password, work_dir)
        ydl.login()

    def test_tk(self):
        import tkinter
        top = tkinter.Tk()
        # 进入消息循环
        top.mainloop()

    def test_t2(self):
        import tkinter as tk

        root = tk.Tk()
        image = tk.PhotoImage(file="../imgs/demo.gif")
        # image = tk.PhotoImage(file="../imgs/demo.gif", format="gif -index 10")
        label = tk.Label(image=image)
        label.pack()
        root.mainloop()

    def test_pysimplegui(self):
        import PySimpleGUI as sg

        sg.theme('DarkAmber')  # Add a touch of color
        # All the stuff inside your window.
        layout = [[sg.Text('Some text on Row 1')],
                  [sg.Text('Enter something on Row 2'), sg.InputText()],
                  [sg.Button('Ok'), sg.Button('Cancel')]]

        # Create the Window
        window = sg.Window('Window Title', layout)
        # Event Loop to process "events" and get the "values" of the inputs
        while True:
            event, values = window.read()
            if event in (None, 'Cancel'):  # if user closes window or clicks cancel
                break
            print('You entered ', values[0])

        window.close()

    def test_pysimplegui2(self):
        import PySimpleGUI as sg
        from PIL import Image, ImageTk, ImageDraw

        def draw_frame(target, im_path):  # Point
            """double-buffered redraw of the background image and the red rubber-rect"""
            img = Image.open(im_path).convert("RGBA")
            # img = Image.open(im_path)
            # ImageTk.PhotoImage(im_path)

            b = io.BytesIO()
            img.save(b, 'PNG')

            target.DrawImage(data=b.getvalue(), location=(200, 200))

        layout = [
            [
                sg.Graph(
                    canvas_size=(400, 400),
                    graph_bottom_left=(0, 0),
                    graph_top_right=(400, 400),
                    key="graph"
                )
            ],
            [sg.Text('Enter something on Row 2'), sg.InputText()],
        ]

        window = sg.Window("请输入验证码", layout)
        window.Finalize()

        graph = window.Element("graph")
        # im = Image.open("../imgs/verify.jpg")
        # buffer = im.copy()
        # b = io.BytesIO()
        # buffer.save(b, 'PNG')


        # graph.DrawImage(filename="../imgs/get.jpg", location=(0, 400))
        # graph.DrawImage(filename="../imgs/get.jpg", location=(0, 400))
        # graph.DrawImage(data=b.getvalue(), location=(0, 400))
        draw_frame(graph, "../imgs/verify.jpg")
        # graph.DrawRectangle((200, 200), (250, 300), line_color="red")

        while True:
            event, values = window.Read()
            if event is None:
                break

    def test_pysimplegui(self):
        import PySimpleGUI as sg
        from PIL import Image

        def draw_frame(target, im_path):
            img = Image.open(im_path).convert("RGBA")
            b = io.BytesIO()
            img.save(b, 'PNG')

            target.DrawImage(data=b.getvalue(), location=(0, 30))

        layout = [
            [
                sg.Graph(
                    canvas_size=(80, 30),
                    graph_bottom_left=(0, 0),
                    graph_top_right=(80, 30),
                    key="graph",
                    enable_events=True
                    # background_color="red"
                )
            ],
            [sg.Text('请输入验证码,点击图片切换')],
            [sg.InputText(size=(25, 1)), sg.Button("确定")],
        ]

        window = sg.Window("验证码", layout)
        window.Finalize()

        graph = window.Element("graph")
        draw_frame(graph, "../imgs/verify.jpg")

        while True:
            event, values = window.Read()
            if event == "graph":
                pass
            if event is None:
                break

    def test_remi(self):
        pass

    def test_Graph_Drawing(self):
        import PySimpleGUI as sg
        from PIL import Image
        layout = [[sg.Graph(canvas_size=(400, 400), graph_bottom_left=(0, 0), graph_top_right=(400, 400),
                            background_color='red', enable_events=True, key='graph')],
                  [sg.Text('Change circle color to:'), sg.Button('Red'), sg.Button('Blue'), sg.Button('Move')]]

        window = sg.Window('Graph test', layout, finalize=True)

        graph = window['graph']
        circle = graph.draw_circle((75, 75), 25, fill_color='black', line_color='white')
        point = graph.draw_point((75, 75), 10, color='green')
        oval = graph.draw_oval((25, 300), (100, 280), fill_color='purple', line_color='purple')
        rectangle = graph.draw_rectangle((25, 300), (100, 280), line_color='purple')
        line = graph.draw_line((0, 0), (100, 100))
        arc = graph.draw_arc((0, 0), (400, 400), 160, 10, style='arc', arc_color='blue')
        while True:
            event, values = window.read()
            print(event, values)
            if event is None:
                break
            if event in ('Blue', 'Red'):
                graph.TKCanvas.itemconfig(circle, fill=event)
            elif event == 'Move':
                graph.MoveFigure(point, 10, 10)
                graph.MoveFigure(circle, 10, 10)
                graph.MoveFigure(oval, 10, 10)
                graph.MoveFigure(rectangle, 10, 10)
                graph.MoveFigure(arc, 10, 10)

        window.close()


    def test_PySimpleGUI():
        import PySimpleGUI as sg
    
        sg.theme('DarkAmber')  # Add a touch of color
        # All the stuff inside your window.
        layout = [[sg.Text('Some text on Row 1')],
                  [sg.Text('Enter something on Row 2'), sg.InputText()],
                  [sg.Button('Ok'), sg.Button('Cancel')]]
    
        # Create the Window
        window = sg.Window('Window Title', layout)
        # Event Loop to process "events" and get the "values" of the inputs
        while True:
            event, values = window.read()
            if event in (None, 'Cancel'):  # if user closes window or clicks cancel
                break
            print('You entered ', values[0])
    
        window.close()

    def test_rename(self):
        os.rename("../build/exe.win32-3.8/lib/Tkinter", "../build/exe.win32-3.8/lib/tkinter")
if __name__ == '__main__':
    unittest.main()
